package com.cleaningcloud.cld.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class SalaryCalculatorUtils {

    public static void main(String[] args) {
        SalaryCalculatorUtils salaryCalculator = new SalaryCalculatorUtils();
        BigDecimal totalSalary = new BigDecimal("30000"); // 假设当月工资总和为30000
        LocalDate startDate = LocalDate.of(2023, 3, 1); // 假设月份为2023年3月
        LocalDate endDate = LocalDate.of(2023, 3, 31);

        BigDecimal dailyAverageSalary = salaryCalculator.calculateDailyAverageSalary(totalSalary, startDate, endDate);
        System.out.println("每天的平均工资: " + dailyAverageSalary);
    }

    public BigDecimal calculateDailyAverageSalary(BigDecimal totalSalary, LocalDate startDate, LocalDate endDate) {
        long daysInMonth = ChronoUnit.DAYS.between(startDate, endDate.plusDays(1));
        System.out.println(daysInMonth - 1);
        BigDecimal dailyAverage = totalSalary.divide(BigDecimal.valueOf(daysInMonth), 2, RoundingMode.HALF_UP);

        // 修正最后一天的工资，确保总工资不超过当月总工资
        BigDecimal lastDaySalary = totalSalary.subtract(dailyAverage.multiply(BigDecimal.valueOf(daysInMonth - 1)));
        System.out.println(dailyAverage + ", " + lastDaySalary);
        return dailyAverage.add(lastDaySalary);
    }

}
